package com.bootdo.clouddo.service.douban.impl;

import com.bootdo.clouddo.dao.douban.DbBookDetailMapper;
import com.bootdo.clouddo.domain.douban.DbBookDetail;
import com.bootdo.clouddo.service.douban.IDbBookDetailService;
import com.bootdo.clouddo.utils.douban.DbBookUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class DbBookDetailServiceImpl implements IDbBookDetailService {

    @Autowired
    private DbBookDetailMapper dao;

    @Override
    public int saveOrUpdate(DbBookDetail record) {
        if(findById(record.getId()) != null)
            return dao.updateByPrimaryKeySelective(record);
        else
            return dao.insertSelective(record);
    }

    @Override
    public DbBookDetail findById(Long id) {
        return dao.selectByPrimaryKey(id);
    }

    @Override
    public DbBookDetail getRandom() {
        Long id = DbBookUtil.getRandomId();
        if(id == null || id < 0)
            return dao.getRandom();
        return dao.selectByPrimaryKey(id);
    }

    @Override
    public List<Long> getAllIds() {
        return dao.getAllIds();
    }

    @Override
    public List<DbBookDetail> find(String keyword) {
        return dao.find(keyword);
    }
}
